<?php

/**
 * @Author: liu
 * @Date:   2018-01-03 16:25:21
 * @Last Modified by:   liu
 * @Last Modified time: 2018-01-08 11:09:14
 * 商品推荐策略
 */
class SupplyRecommend extends Eloquent
{
    protected $table = "supply_recommend";



    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-05T14:12:06+0800
     * @param    [object]                   $car_model          [车型]
     * @return   [array]                                       [返回耗材id数组]
     *逻辑如下
     *1 查看该车型的 推荐策略 如果不再存在 查找该车系的如果还不存在查找该品牌的 推荐策略   如果 传空 就是查看没有车型信息的推荐策略
     *2 根据查出的推荐策略 按照耗材分类 分组 分别找出改分组的 耗材id
     *3 找出分组的 逻辑如下。同分组的 优先查看 精确到耗材id的  都不存在id的  查看系列 包含的id 如果还不存在 则看 品牌
     */
    public static function getRecommendByCarModel($car_model){
    	/**
    	 * step 1 查找使用的策略分组
    	 */
        if($car_model){
            $sr=SupplyRecommend::where("car_model_id",$car_model->id)->where("status",1)->orderBy("s_sc3","desc")->orderBy("supply_brand_id","desc")->orderBy("supply_series_id","desc")->orderBy("supply_id","desc")->get();
            if(sizeof($sr)==0){
                $sr=SupplyRecommend::where("car_series_id",$car_model->series_name_id)->where("car_year",$car_model->model_year)->where("status",1)->orderBy("s_sc3","desc")->orderBy("supply_brand_id","desc")->orderBy("supply_series_id","desc")->orderBy("supply_id","desc")->get();
                if(sizeof($sr)==0){
                    $sr=SupplyRecommend::where("car_series_id",$car_model->series_name_id)->where("status",1)->orderBy("s_sc3","desc")->orderBy("supply_brand_id","desc")->orderBy("supply_series_id","desc")->orderBy("supply_id","desc")->get();
                    if(sizeof($sr)==0){
                        $sr=SupplyRecommend::where("car_series_id",$car_model->series_name_id)->where("status",1)->orderBy("s_sc3","desc")->orderBy("supply_brand_id","desc")->orderBy("supply_series_id","desc")->orderBy("supply_id","desc")->get();
                    }
                }
            }
        }else{//如果是找不存在车型信息的
            $sr=SupplyRecommend::where("car_brand_id",0)->where("status",1)->orderBy("s_sc3","desc")->orderBy("supply_brand_id","desc")->orderBy("supply_series_id","desc")->orderBy("supply_id","desc")->get();
        }
    	/**
    	 * step 2 根据推荐策略分组
    	 */
    	
    	$supply_classification_arr=array();
    	foreach ($sr as $_sr) {
    		if(isset($supply_classification_arr[$_sr->s_sc3])){
    			if($_sr->supply_id!=0){																	//说明该规则指定耗材
	        		$supply_classification_arr[$_sr->s_sc3]["supply_id"][]=$_sr->supply_id;
	        	}else if($_sr->supply_series_id!=0){
	        		$supply_classification_arr[$_sr->s_sc3]["supply_series_id"][]=$_sr->supply_series_id;
	        	}else if($_sr->supply_brand_id!=0){
	        		$supply_classification_arr[$_sr->s_sc3]["supply_brand_id"][]=$_sr->supply_brand_id;
	        	}
    		}else{
    			if($_sr->supply_id!=0){																	//说明该规则指定耗材
	        		$supply_classification_arr[$_sr->s_sc3]=array("supply_id"=>array($_sr->supply_id),"supply_series_id"=>array(),"supply_brand_id"=>array());
	        	}else if($_sr->supply_series_id!=0){
	        		$supply_classification_arr[$_sr->s_sc3]=array("supply_id"=>array(),"supply_series_id"=>array($_sr->supply_series_id),"supply_brand_id"=>array());
	        	}else if($_sr->supply_brand_id!=0){
	        		$supply_classification_arr[$_sr->s_sc3]=array("supply_id"=>array(),"supply_series_id"=>array(),"supply_brand_id"=>array($_sr->supply_brand_id));
	        	}
    		}
    	}
    	/**
    	 * 够造最后的返回耗材id
    	 */
    	$finally=array();
    	foreach ($supply_classification_arr as $_sca) {
    		if(sizeof($_sca["supply_id"])>0){
    			$finally=array_merge($finally,$_sca["supply_id"]);
    		}else if(sizeof($_sca["supply_series_id"])>0){
                $fid=SupplyItem::whereIn("series_id",$_sca["supply_series_id"])->select("id")->get();
                foreach ($fid as $_f) {
                    $finally[]=$_f->id;
                }
    		}else if(sizeof($_sca["supply_brand_id"])>0){
                $fid=SupplyItem::whereIn("brand_id",$_sca["supply_brand_id"])->select("id")->get();
                foreach ($fid as $_f) {
                    $finally[]=$_f->id;
                }
    		}
    	}
    	return array_unique($finally);   	
    }   
}